#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "game.h"
#include "print.h"
#include "judge.h"


void game(int num, int resum, char* name1, char* name2, char a[100][100])
{
	//下棋

	int n=1;
	int p,q,p0,q0;
	int r=0;

	while(r!=1)
	{
		if(n%2==1)
		{
			printf("第%d步，先手%s下，符号为‘X’，输入棋子坐标，格式为: x y\n",n,name1);
			//printf("%d\n",n);
			//printf("%s\n",name1);
		}
		else if(n%2==0)
		{
			printf("第%d步，后手%s下，符号为‘O’，输入棋子坐标，格式为: x y\n",n,name2);
		}
		scanf("%d %d", &p0,&q0);
		
		int regret1=0;
		int regret2=0;
		
		//悔棋设置
		if(p0==0&&q0==0)
		{
			if(n==1)
				printf("不在棋盘范围内，请重新输入\n");
			else if(regret1<resum&&n%2==0)
			{
				n--;
				a[p][q]='.';
				regret1++;
				printf("先手%s悔棋%d步\n",name1,regret1);
			}
			else if(regret2<resum&&n%2==1)
			{
				n--;
				a[p][q]='.';
				regret2++;
				printf("后手%s悔棋%d步\n",name2,regret2);
			}
			else if(regret1>=resum)	
			{
				printf("先手%s已用完悔棋步数！\n",name1);
			}	
			else if(regret2>=resum)	
			{
				printf("后手%s已用完悔棋步数！\n",name2);
			}	
			continue;
		}
		
		if(p0!=0&&q0!=0)
		{
			p=p0-1;
			q=q0-1;
		}
		
			
		if(p<0||p>=num||q<0||q>=num)//检验落子范围
		{
			printf("不在棋盘范围内，请重新输入\n");
			continue;
		}
		if(a[p][q]=='.'&&n%2==1)
			a[p][q]='X';
		else if(a[p][q]=='.'&&n%2==0)
			a[p][q]='O';
		else if(a[p][q]=='X'||a[p][q]=='O')
		{
			printf("重复落子，请重新输入\n");
			continue;
		}
		
		print(num,a);
		
		r = judge(num,n,p,q,a);
		
		n++;
	}
	
	if(n<num*num+1&&n%2==0)
		printf("先手%s获胜\n",name1);
	else if(n<num*num+1&&n%2==1)
		printf("后手%s获胜\n",name2);
}